﻿Imports System.IO

'************************************************************************************************************************
'* Name:   WorkOrderFacade
'* Description: Facade / Service Layer Implementation for WorkOrder
'* Created On:  03-DEC-2010
'* Created By:  Senthilnathan karuppaiah
'* Last Modified On:
'* Last Modified By:
'* Last Modified Reason:
'************************************************************************************************************************
''' <summary>
''' Facade / Service Layer Implementation for WorkOrder
''' </summary>
''' <remarks></remarks>
Public Class WorkOrderFacade
    Implements IWorkOrderFacade

#Region "Methods"
    ''' <summary>
    ''' Ugly hack to avoid printing blank page if the number of lines between 35 and 59 (According to the report one I am using)
    ''' Maximum # of lines in first page = 34
    ''' Maximum number of lines in the subsequent page (other than first page) = 59
    ''' </summary>
    ''' <param name="description"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ManipulateDescriptionToAvoidBlankPageInReports(ByVal description As String) As String
        If Not String.IsNullOrEmpty(description) Then
            Dim descLines As String() = description.Split(CChar("*****"))
            Dim lineCount As Integer = 0
            For Each s In descLines
                If Not String.IsNullOrEmpty(s) Then
                    lineCount = lineCount + 1
                End If
            Next
            If lineCount > 34 AndAlso lineCount <= 59 Then
                Dim i As Integer
                For i = lineCount To 60
                    description = description & Environment.NewLine
                Next
            End If
        End If
        Return description
    End Function
    ''' <summary>
    ''' TODO:
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetWorkOrders() As System.Collections.Generic.List(Of DtoWorkOrder) Implements IWorkOrderFacade.GetWorkOrders
        Dim wos As List(Of DtoWorkOrder) = Nothing
        Dim xmlserializer As New System.Xml.Serialization.XmlSerializer(GetType(List(Of DtoWorkOrder)))
        Dim path As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
        Dim dataFile As String = path.Replace("file:\", "") & "\data\" & "dtoworkorder.xml"
        Dim fs As New System.IO.FileStream(dataFile, IO.FileMode.Open)
        wos = CType(xmlserializer.Deserialize(fs), List(Of DtoWorkOrder))
        'Ugly hack to avoid printing blank page if the number of lines between 35 and 59 (According to the report one I am using)
        'Comment this loop to see the blank page printed for the second report in the collection
        For Each wo In wos
            wo.Description = ManipulateDescriptionToAvoidBlankPageInReports(wo.Description)
        Next
        fs.Close()
        Return wos
    End Function

    ''' <summary>
    ''' TODO:
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetWorkOrders(ByVal status As String) As System.Collections.Generic.List(Of DtoWorkOrder) Implements IWorkOrderFacade.GetWorkOrders
        Throw New NotImplementedException
    End Function

#End Region 'Methods

    Public Function GetWorkOrderByNumber(ByVal workOrderNumber As Decimal) As DtoWorkOrder Implements IWorkOrderFacade.GetWorkOrderByNumber
        Throw New NotImplementedException
    End Function
End Class